package cn.com.thingo.hyintelligent.ui.fragment;

import android.content.IntentFilter;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.th.opensdk.openapi.NotificationInfo;
import com.th.opensdk.openapi.SceneGroupInfo;
import com.th.opensdk.openapi.SceneInfo;
import com.th.opensdk.openapi.THOpenSDK;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import cn.com.thingo.hyintelligent.R;
import cn.com.thingo.hyintelligent.app.App;
import cn.com.thingo.hyintelligent.base.RootFragment;
import cn.com.thingo.hyintelligent.base.contract.ScenesContract;
import cn.com.thingo.hyintelligent.presenter.ScenesPresenter;
import cn.com.thingo.hyintelligent.ui.adapter.ScenesListAdapter;
import cn.com.thingo.hyintelligent.ui.adapter.decoration.RecycleViewDivider;
import cn.com.thingo.hyintelligent.ui.receiver.THOpenReceiver;
import cn.com.thingo.hyintelligent.util.ToastUtil;

public class ScenesFragment extends RootFragment<ScenesPresenter> implements ScenesContract.View, ScenesListAdapter.OnItemClickListener {
    @BindView(R.id.scenes_rv)
    RecyclerView rvList;
    private List<SceneInfo> list = new ArrayList<>();
    private ScenesListAdapter adapter;
    private THOpenReceiver receiver;

    public ScenesFragment() {
    }

    public static ScenesFragment newInstance() {
        return new ScenesFragment();
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        initView();
        initData();
        initReceiver();
    }

    private void initReceiver() {
        receiver = new THOpenReceiver();
        IntentFilter filter = new IntentFilter();
        filter.addAction(NotificationInfo.BROADCAST_DEVICE_STATE_UPLOAD);
        mContext.registerReceiver(receiver, filter);
    }

    private void initData() {
        List<SceneGroupInfo> sceneGroupList = THOpenSDK.getInstance().getSceneGroupList();
        for (int i = 0; i < sceneGroupList.size(); i++) {
            List<SceneInfo> sceneList = sceneGroupList.get(i).getSceneList();
            if (sceneList.size() > 0) {
                for (int j = 0; j < sceneList.size(); j++) {
                    if (!sceneList.get(j).getSceneName().startsWith("窗户-")) {
                        list.add(sceneList.get(j));
                    }
                }
            }
        }
        if (list.size() > 0) {
            adapter.notifyDataSetChanged();
        }
    }

    private void initView() {
        rvList.setLayoutManager(new LinearLayoutManager(mContext));
        adapter = new ScenesListAdapter(list, mContext);
        rvList.addItemDecoration(new RecycleViewDivider(mContext, LinearLayoutManager.VERTICAL, 1, mContext.getResources().getColor(R.color.gray_12)));
        adapter.setListener(this);
        rvList.setAdapter(adapter);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_scenes;
    }

    @Override
    protected void initInject() {
        getFragmentComponent().inject(this);
    }

    @Override
    public void onItemClick(int position) {
        if (App.getInstance().isScenesStatus()) {
            //场景开关控制
            App.getInstance().setScenesStatus(false);
            final SceneInfo sceneInfo = list.get(position);
            long controlId = sceneInfo.getControlId();
            App.getInstance().setCurrentScenesId(controlId);
            new Thread() {
                @Override
                public void run() {
                    super.run();
                    sceneInfo.run();
                }
            }.start();
        } else {
            ToastUtil.shortShow("有其他场景正在部署中...");
        }
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        mContext.unregisterReceiver(receiver);
    }
}
